package com.qiange.studentmanage.controller;

import com.qiange.studentmanage.Exception.AgeOutOfBoundsException;
import com.qiange.studentmanage.Exception.WrongFormatException;
import com.qiange.studentmanage.pojo.Student;
import com.qiange.studentmanage.service.IStudentService;
import com.qiange.studentmanage.service.impl.StudentServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    private IStudentService studentService = new StudentServiceImpl();
    Scanner sc = new Scanner(System.in);
    Student student = new Student();

    public void start() {
        boolean flag = true;
        while (flag) {
            System.out.println("欢迎访问学生管理系统");
            System.out.println("1. 添加");
            System.out.println("2. 查看");
            System.out.println("3. 删除");
            System.out.println("4. 修改");
            System.out.println("5. 退出");

            System.out.println("请输入您的选择");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    add();
                    break;
                case "2":
                    findAll();
                    break;
                case "3":
                    remove();
                    break;
                case "4":
                    set();
                    break;
                case "5":
                    System.out.println("感谢使用");
                    flag = false;
                    break;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }

    }

    private void set() {
        int index = 0;
        String sid = null;
        while (true){
            try {
                System.out.println("请输入学号");
                sid = sc.next();
                student.setSid(sid);
                index= studentService.getIndexById(sid);
                if (index== -1) {
                    System.out.println("该学号不存在，请重新输入");
                    continue;
                }
            } catch (WrongFormatException e) {
                e.printStackTrace();
                continue;
            }
            Student student = inputStudentInfo(sid);
            if (student == null) continue;
            boolean add = studentService.set(index,student);
            if (add) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
            break;
        }

    }


    private void remove() {
        int index;
        while (true){
            try {
                System.out.println("请输入学号");
                String sid = sc.next();
                student.setSid(sid);
                index= studentService.getIndexById(sid);
                if (index== -1) {
                    System.out.println("该学号不存在，请重新输入");
                    continue;
                }else {
                    break;
                }
            } catch (WrongFormatException e) {
                e.printStackTrace();
            }
        }
        boolean remove = studentService.remove(index);
        if (remove) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }

    private void findAll() {
        ArrayList<Student> list = studentService.findAll();
        if (list==null){
            System.out.println("暂无信息，请添加后再查询");
            return;
        }
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

    private void add() {
        String sid;
        while (true) {
            try {
                System.out.println("请输入学号");
                sid = sc.next();
                student.setSid(sid);
                int index = studentService.getIndexById(sid);
                if (index != -1) {
                    System.out.println("该学号已存在，请重新输入");
                    continue;
                }

            } catch (WrongFormatException e) {
                e.printStackTrace();
                continue;
            }
            Student student = inputStudentInfo(sid);
            if (student == null) continue;
            boolean add = studentService.add(student);
            if (add) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
            break;
        }

    }

    private Student inputStudentInfo(String sid) {
        String name;
        int age;
        String birthday;
        try {
            System.out.println("请输入姓名");
            name = sc.next();
            student.setName(name);
        } catch (WrongFormatException e) {
            e.printStackTrace();
            return null;
        }
        try {
            System.out.println("请输入年龄");
            age = sc.nextInt();
            student.setAge(age);
        } catch (AgeOutOfBoundsException e) {
            e.printStackTrace();
            return null;
        }
        try {
            System.out.println("请输入生日,格式为yyyy年MM月dd日");
            birthday = sc.next();
            student.setBirthday(birthday);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("格式错误");
            return null;
        }
        Student student = new Student(sid, name, age, birthday);
        return student;
    }
}
